//Design By Do Trung --- Email : trungqn19852003@yahoo.com
//Ngày Design :29/03/2012 10:34:13 AM
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using Savico.Library.Commom.SqlServer;
using Entity = Savico.Library.Entity;
namespace Savico.Library.Data
{
	/// <summary>
	/// </summary>
	public class LinhVucKinhDoanh
{
		public LinhVucKinhDoanh() {}
				Connection db = new Connection();

/// <summary>
/// Inserts a record into the LinhVucKinhDoanh table.
/// <summary>
/// <param name="IdLinhVuc"></param>
/// <param name="NgonNgu"></param>
/// <param name="TenLinhVuc"></param>
/// <param name="HinhAnhLinhVuc"></param>
/// <returns></returns>
	public bool Insert(Entity.LinhVucKinhDoanh entity)
	{
		bool boolInsert = false;
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_LinhVucKinhDoanh_Insert";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlCmd.Parameters.Add("@IdLinhVuc", SqlDbType.Int).Value=entity.IdLinhVuc;
			SqlCmd.Parameters.Add("@NgonNgu", SqlDbType.VarChar,2).Value=entity.NgonNgu;
			SqlCmd.Parameters.Add("@TenLinhVuc", SqlDbType.NVarChar,100).Value=entity.TenLinhVuc;
			SqlCmd.Parameters.Add("@HinhAnhLinhVuc", SqlDbType.NVarChar,100).Value=entity.HinhAnhLinhVuc;

			SqlAdp.InsertCommand = SqlCmd;
			SqlAdp.InsertCommand.ExecuteNonQuery();
			SqlCmd.Transaction.Commit();
			boolInsert = true;
			SqlCnn.Close();
		}
		catch
		{
			boolInsert = false;
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return boolInsert;
	}

/// <summary>
/// Selects a single record from the LinhVucKinhDoanh table.
/// <summary>
/// <returns>DataTable</returns>
	public DataTable Select()
		{
		DataTable dt = new DataTable();
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_LinhVucKinhDoanh_Select";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlAdp.SelectCommand = SqlCmd;
			SqlAdp.SelectCommand.ExecuteNonQuery();
			SqlAdp.Fill(dt);
			SqlCmd.Transaction.Commit();
			SqlCnn.Close();
		}
		catch
		{
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return dt;
	}

/// <summary>
/// Selects a single record from the LinhVucKinhDoanh table.
/// <summary>
/// <returns>DataTable</returns>
	public DataTable SelectPrimaryKey(Entity.LinhVucKinhDoanh entity)
	{
		DataTable dt = new DataTable();
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_LinhVucKinhDoanh_SelectPrimaryKey";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlCmd.Parameters.Add("@IdLinhVuc", SqlDbType.Int).Value=entity.IdLinhVuc;
			SqlCmd.Parameters.Add("@NgonNgu", SqlDbType.VarChar,2).Value=entity.NgonNgu;

			SqlAdp.SelectCommand = SqlCmd;
			SqlAdp.SelectCommand.ExecuteNonQuery();
			SqlAdp.Fill(dt);
			SqlCmd.Transaction.Commit();
			SqlCnn.Close();
		}
		catch
		{
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return dt;
	}
    public DataTable SelectTheoNgonNgu(Entity.LinhVucKinhDoanh entity)
    {
        DataTable dt = new DataTable();
        SqlConnection SqlCnn = db.ReadFromWebConfig();
        SqlDataAdapter SqlAdp = new SqlDataAdapter();
        SqlCommand SqlCmd = new SqlCommand();
        SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
        try
        {
            SqlCmd.Transaction = SqlTrans;
            SqlCmd.Connection = SqlCnn;
            SqlCmd.CommandText = "Proc_LinhVucKinhDoanh_SelectTheoNgonNgu";
            SqlCmd.CommandType = CommandType.StoredProcedure;            
            SqlCmd.Parameters.Add("@NgonNgu", SqlDbType.VarChar, 2).Value = entity.NgonNgu;

            SqlAdp.SelectCommand = SqlCmd;
            SqlAdp.SelectCommand.ExecuteNonQuery();
            SqlAdp.Fill(dt);
            SqlCmd.Transaction.Commit();
            SqlCnn.Close();
        }
        catch
        {
            SqlCmd.Transaction.Rollback();
        }
        finally
        {
            SqlCmd.Parameters.Clear();
            SqlTrans = null;
        }
        if (SqlCmd != null)
            SqlCmd.Dispose();
        if (SqlAdp != null)
            SqlAdp.Dispose();
        return dt;
    }

/// <summary>
/// Updates a record in the LinhVucKinhDoanh table.
/// <summary>
/// <param name="IdLinhVuc"></param>
/// <param name="NgonNgu"></param>
/// <param name="TenLinhVuc"></param>
/// <param name="HinhAnhLinhVuc"></param>
	public bool Update(Entity.LinhVucKinhDoanh entity)
	{
		bool boolUpdate = false ;
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_LinhVucKinhDoanh_Update";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlCmd.Parameters.Add("@IdLinhVuc", SqlDbType.Int).Value=entity.IdLinhVuc;
			SqlCmd.Parameters.Add("@NgonNgu", SqlDbType.VarChar,2).Value=entity.NgonNgu;
			SqlCmd.Parameters.Add("@TenLinhVuc", SqlDbType.NVarChar,100).Value=entity.TenLinhVuc;
			SqlCmd.Parameters.Add("@HinhAnhLinhVuc", SqlDbType.NVarChar,100).Value=entity.HinhAnhLinhVuc;

			SqlAdp.UpdateCommand = SqlCmd;
			SqlAdp.UpdateCommand.ExecuteNonQuery();
			SqlCmd.Transaction.Commit();
			boolUpdate = true ;
			SqlCnn.Close();
		}
		catch
		{
			boolUpdate = false ;
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return boolUpdate;
	}

/// <summary>
		/// Deletes a record from the LinhVucKinhDoanh table by a composite primary key.
/// <summary>
	public bool Delete(Entity.LinhVucKinhDoanh entity)
	{
		bool boolDelete = false;
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_LinhVucKinhDoanh_Delete";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlCmd.Parameters.Add("@IdLinhVuc", SqlDbType.Int).Value=entity.IdLinhVuc;
			SqlCmd.Parameters.Add("@NgonNgu", SqlDbType.VarChar,2).Value=entity.NgonNgu;

			SqlAdp.DeleteCommand = SqlCmd;
			SqlAdp.DeleteCommand.ExecuteNonQuery();
			SqlCmd.Transaction.Commit();
			boolDelete = true ;
			SqlCnn.Close();
		}
		catch
		{
			boolDelete = false ;
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return boolDelete;
	}
	}
}
